import requests
import time 
import json

headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
         'Content-Type':'application/x-www-form-urlencoded'}
s=requests.session()
s.headers.update(headers)
r=s.post('http://10.1.20.49:7002/itsm/xhrs_am_login.jsp', data={'j_username':'lijy_wb','j_password':'123456'}, headers=headers)

count=0

while True:
  try:
    print('')
    count += 1
    postdata={'flowCodes':'ALL','id':'100149','limit':'0'}
    r=s.post('http://10.1.20.49:7002/itsm/xhrsFilterconditionAction!allFlowCount.action?_timestamp=' + str(time.time()*100000000),data=postdata,headers=headers)
    j=json.loads(str(r.content, 'utf-8'))
    # print(j)
    # print(j.keys())
    inc_count = j['data']['INCIDENT']
    srm_count = j['data']['SERVICE_INFO']
    print('事件工单个数：',inc_count,'服务工单个数：',srm_count,'总数：',j['data']['total'])
    postdata={'id':'100149','flowCodes':'INCIDENT','consoleType':'NEW','start':'0','limit':inc_count}
    r=s.post('http://10.1.20.49:7002/itsm/xhrsFilterconditionAction!allFlowFiltercondition.action?_timestamp=' + str(time.time()*100000000), data=postdata,headers=headers)
    #print(str(r.content, 'utf-8'))
    j=json.loads(str(r.content, 'utf-8'))
    print(str(count) + '<--------------------lijy_wb事件工单查询------------------->')
    for i in j['root']:
      if '新华官网' == i['subSort'] or \
         '掌上新华系统' == i['subSort'] or \
         '柜面自助服务平台' == i['subSort'] or \
         '移动保全系统2.0' == i['subSort'] or \
         '新电话中心' == i['subSort']:
          continue
      print('['+i['subSort']+']',i['topic'],i['no'])
    postdata={'id':'100149','flowCodes':'SERVICE_INFO','consoleType':'NEW','start':'0','limit':srm_count}
    r=s.post('http://10.1.20.49:7002/itsm/xhrsFilterconditionAction!allFlowFiltercondition.action?_timestamp=' + str(time.time()*100000000),data=postdata,headers=headers)
    #print(str(r.content, 'utf-8'))
    j=json.loads(str(r.content, 'utf-8'))
    print(str(count) + '<--------------------lijy_wb服务工单查询------------------->')
    for i in j['root']:
      print(i['no'],i['topic'])
    time.sleep(30)
  except requests.exceptions.ConnectionError:
    print('ConnectionError -- please wait 3 seconds')
    time.sleep(3)
  except requests.exceptions.ChunkedEncodingError:
    print('ChunkedEncodingError -- please wait 3 seconds')
    time.sleep(3)    
  except:
    print('Unfortunitely -- An Unknow Error Happened, Please wait 3 seconds')
    time.sleep(3)



# with open('记录.txt','w') as f:
  # f.write(str(r.content, 'utf-8'))